<%@ page import="com.pijunn.label.Label" %>
<!DOCTYPE html>
<html>
	<head>
		<meta name="layout" content="main">
		<g:set var="entityName" value="${message(code: 'label.label', default: 'Label Inventory')}" />
		<title><g:message code="default.assign.label" args="[entityName]" /></title>
	</head>
	<body>
		<a href="#create-label" class="skip" tabindex="-1"><g:message code="default.link.skip.label" default="Skip to content&hellip;"/></a>
		<div class="nav" role="navigation">
			<ul>
				<li><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></li>
				<li><g:link class="list" action="list"><g:message code="default.list.label" args="[entityName]" /></g:link></li>
			</ul>
		</div>
		<div id="create-label" class="content scaffold-create" role="main">
			<h1><g:message code="default.create.label" args="[entityName]" /></h1>
			<g:if test="${flash.message}">
			<div class="message" role="status">${flash.message}</div>
			</g:if>
			<g:hasErrors bean="${cmd}">
			<ul class="errors" role="alert">
				<g:eachError bean="${cmd}" var="error">
				<li <g:if test="${error in org.springframework.validation.FieldError}">data-field-id="${error.field}"</g:if>><g:message error="${error}"/></li>
				</g:eachError>
			</ul>
			</g:hasErrors>
			<g:form action="assign" >
				<fieldset class="form">	
					<g:hiddenField id="user_id" name="user.id"/>
					<div class="fieldcontain ${hasErrors(bean: cmd, field: 'user', 'error')} required">
						<label for="user">
							<g:message code="label.user.label" default="User" />
							<span class="required-indicator">*</span>
						</label>
						<g:textField name="user" value="${user}" required=""/>
					</div>
					<div class="fieldcontain ${hasErrors(bean: cmd, field: 'serialNumber', 'error')} required">
						<label for="startSerialNumber">
							<g:message code="label.startSerialNumber.label" default="Starting Serial Number" />
							<span class="required-indicator">*</span>
						</label>
						<g:field name="startSerialNumber" type="number" value="${startSerialNumber}" required="" maxlength="8" size="8"/>
					</div>
					
					<div class="fieldcontain ${hasErrors(bean: cmd, field: 'serialNumber', 'error')} required">
						<label for="endSerialNumber">
							<g:message code="label.startSerialNumber.label" default="Ending Serial Number" />
							<span class="required-indicator">*</span>
						</label>
						<g:field name="endSerialNumber" type="number" value="${endSerialNumber}" required="" maxlength="8" size="8"/>
					</div>
				</fieldset>
				<fieldset class="buttons">
					<g:submitButton name="assign" class="save" value="${message(code: 'default.button.assign.label', default: 'Assign')}" />
				</fieldset>
			</g:form>
		</div>
	</body>
	<g:javascript>
	$(function(){
		$('#assign').click(function(){
			var count = parseInt($('#endSerialNumber').val()) - parseInt($('#startSerialNumber').val()) + 1;
			if (confirm('Yau are assigning ' + count + ' new labels from inventory.  Is this correct?')) {
				return true;
			}
			
			return false;
		});
		
		var url = "${createLink(controller:'appUser', action:'searchJson')}/"
		var items = [];
		$( "#user" ).autocomplete({
            source: function( request, response ) {
                $.ajax({
                    url: url + request.term,
                    success: function( data ) {
                        response( $.map( data, function( item ) {
                        	items.push({name:item.firstName + " " + item.lastName, id: item.id})
                            return item.firstName + " " + item.lastName
                         }));
                    }
                });
            },
            minLength: 2,
            select: function( event, ui ) {
            	for (var i=0, j=items.length; i < j; i++) {
            		if (ui.item.label == items[i].name) {
            			$('#user_id').val(items[i].id);
           				break;
            		}
            	}
            }
        });
	});
	</g:javascript>
</html>
